import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

/**
 * 
 * @author Michael Zipp
 * @version 1.0
 */
public class ThunderDragonHospitalMain {
	static OverFrame mainFrame = new OverFrame();

	static ThunderDragonFrame mainPnl = new ThunderDragonFrame();
	
	static NursePanel nurse;
	
	static PatientPanel patient;
	
	static SystemAdminPanel admin;
	
	static DoctorPanel doctor;
	
	static AddUserPanel addUser;
	
	static EditUserPanel editUser;
	
	static AppointmentPanel appoint;
	
	static DeleteUserPanel deleteUser;
	
	static ResetPasswordPanel resAcc;
	
	static PatientInvoicePanel invoice;
	
	static TreatmentRecordPanel createRecord;
	
	static SearchPanel search;
	
	static Component prevPanel;
	
	static User currentUser;

	static LockUserPanel lockAcc;

	static UnlockUserPanel unlockAcc;
	
	static SearchPanel findUser;

	static DoctorAndNurseViewAppointmentPanel viewApps;

	public static PatientViewAppointmentPanel viewAppsPatient;

	public static PatientInvoicePanel viewInvoice;
	
	private static class ButtonListener implements PropertyChangeListener{


		@Override
		public void propertyChange(PropertyChangeEvent arg0) {
			
			if(arg0.getSource().equals(admin)){
				
				if(arg0.getPropertyName().equals("createUser")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					addUser = new AddUserPanel();
					mainPnl.scrollPaneMain.add(addUser);
				}
				else if(arg0.getPropertyName().equals("editUser")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					editUser = new EditUserPanel();
					mainPnl.scrollPaneMain.add(editUser);
				}
				else if(arg0.getPropertyName().equals("deleteUser")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					deleteUser = new DeleteUserPanel();
					mainPnl.scrollPaneMain.add(deleteUser);
					deleteUser.currentUser = currentUser;
				}
				else if(arg0.getPropertyName().equals("resAcc")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					resAcc = new ResetPasswordPanel();
					mainPnl.scrollPaneMain.add(resAcc);
					resAcc.currentUser = currentUser;
				}
				else if(arg0.getPropertyName().equals("lockAcc")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					lockAcc = new LockUserPanel();
					mainPnl.scrollPaneMain.add(lockAcc);
					lockAcc.currentUser = currentUser;
				}
				else if(arg0.getPropertyName().equals("unlockAcc")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					unlockAcc = new UnlockUserPanel();
					mainPnl.scrollPaneMain.add(unlockAcc);
					unlockAcc.currentUser = currentUser;
				}
				else if(arg0.getPropertyName().equals("findUser")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					findUser = new SearchPanel();
					findUser.addPropertyChangeListener(new ButtonListener());
					mainPnl.scrollPaneMain.add(findUser);
				}
			}//arg0.source = admin
			if(arg0.getSource().equals(findUser) 
					&& arg0.getPropertyName().equals("editingUser")){
				if(currentUser.usertype.equals("admin")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					editUser = new EditUserPanel();
					mainPnl.scrollPaneMain.add(editUser);
					editUser.txtUsername.setText(findUser.editingUser.username);
					editUser.txtName.setText(findUser.editingUser.name);
					editUser.txtContactPhone.setText(findUser.editingUser.phoneNum);
					editUser.txtEmailAddress.setText(findUser.editingUser.email);
					editUser.txtPassword.setText(findUser.editingUser.password);
				}
				else{
					findUser.lblError.setText("No Permission");
				}
			}
			if(arg0.getSource().equals(doctor)){
				
				if(arg0.getPropertyName().equals("addPatient")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					addUser = new AddUserPanel();
					mainPnl.scrollPaneMain.add(addUser);
					addUser.comboBox.setEnabled(false);
				}
				else if(arg0.getPropertyName().equals("deletePatient")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					deleteUser = new DeleteUserPanel();
					mainPnl.scrollPaneMain.add(deleteUser);
					deleteUser.comboBox.setEnabled(false);
					deleteUser.currentUser = currentUser;
				}
				else if(arg0.getPropertyName().equals("findPatient")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					findUser = new SearchPanel();
					mainPnl.scrollPaneMain.add(findUser);
					findUser.tabbedPane.setSelectedIndex(4);
					findUser.tabbedPane.setEnabled(false);
				}
				else if(arg0.getPropertyName().equals("createRec") 
						|| arg0.getPropertyName().equals("viewRec")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					createRecord = new TreatmentRecordPanel();
					mainPnl.scrollPaneMain.add(createRecord);
				}
				else if(arg0.getPropertyName().equals("viewSchedule")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					viewApps = new DoctorAndNurseViewAppointmentPanel();
					mainPnl.scrollPaneMain.add(viewApps);
				}
				else if(arg0.getPropertyName().equals("editPatient")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					editUser = new EditUserPanel();
					mainPnl.scrollPaneMain.add(editUser);
				}
				
			}//end arg0.source = doctor

			if(arg0.getSource().equals(nurse)){
				
				if(arg0.getPropertyName().equals("addPatient")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					addUser = new AddUserPanel();
					mainPnl.scrollPaneMain.add(addUser);
					addUser.comboBox.setEnabled(false);
				}
				else if(arg0.getPropertyName().equals("deletePatient")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					deleteUser = new DeleteUserPanel();
					mainPnl.scrollPaneMain.add(deleteUser);
					deleteUser.comboBox.setEnabled(false);
					deleteUser.currentUser = currentUser;
				}
				else if(arg0.getPropertyName().equals("findPatient")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					findUser = new SearchPanel();
					mainPnl.scrollPaneMain.add(findUser);
					findUser.tabbedPane.setSelectedIndex(4);
					findUser.tabbedPane.setEnabled(false);
				}
				else if(arg0.getPropertyName().equals("editPatient")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					editUser = new EditUserPanel();
					mainPnl.scrollPaneMain.add(editUser);
				}
				else if(arg0.getPropertyName().equals("viewSchedule")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					viewApps = new DoctorAndNurseViewAppointmentPanel();
					mainPnl.scrollPaneMain.add(viewApps);
				}
			}//end arg0.source = nurse
			
			if(arg0.getSource().equals(patient)){
				
				if(arg0.getPropertyName().equals("viewHistory")){
					
				}
				else if(arg0.getPropertyName().equals("makeAppointment")){
					
						
				}
				else if(arg0.getPropertyName().equals("viewSchedule")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					viewAppsPatient = new PatientViewAppointmentPanel();
					mainPnl.scrollPaneMain.add(viewAppsPatient);
				}
				else if(arg0.getPropertyName().equals("viewInvoice")){
					prevPanel = mainPnl.scrollPaneMain.getComponents()[0];
					mainPnl.scrollPaneMain.remove(0);
					viewInvoice = new PatientInvoicePanel();
					mainPnl.scrollPaneMain.add(viewInvoice);
				}
			
			}//end arg0.source = patient

			
		}

		public String toString(){
			return "A string for this too.";
		}
	}
	
	private static class LoginListen implements ComponentListener{

		@Override
		public void componentHidden(ComponentEvent arg0) {
			if(arg0.getComponent() == mainFrame.login ){
				currentUser = mainFrame.login.getCurrentUser();
				mainPnl.setVisible(true);
				if(currentUser.getClass().equals((new SystemAdmin()).getClass())){
				
					admin = new SystemAdminPanel();
					admin.addPropertyChangeListener(new ButtonListener());
					mainPnl.scrollPaneMenu.add(admin);
				}
				else if(currentUser.getClass().equals((new Patient()).getClass())){
				
					patient = new PatientPanel();
					patient.addPropertyChangeListener(new ButtonListener());
					mainPnl.scrollPaneMenu.add(patient);
				}
				else if(currentUser.getClass().equals((new Nurse()).getClass())){
					
					nurse = new NursePanel();
					nurse.addPropertyChangeListener(new ButtonListener());
					mainPnl.scrollPaneMenu.add(nurse);
				}
				else if(currentUser.getClass().equals((new Doctor()).getClass())){
					
					doctor = new DoctorPanel();
					doctor.addPropertyChangeListener(new ButtonListener());
					mainPnl.scrollPaneMenu.add(doctor);
				}
			}
			if(arg0.getComponent() == mainPnl){
				
				currentUser = null;
				mainPnl.scrollPaneMain.remove(0);
				mainPnl.scrollPaneMain.add(mainPnl.home);
				mainFrame.login.setVisible(true);
				
			}
		}

		@Override
		public void componentMoved(ComponentEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void componentResized(ComponentEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void componentShown(ComponentEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public String toString(){
			return "A string for the loginListener";
		}
	}
	
	@Override
	public String toString() {
		return "ThunderDragonHospitalMain [getClass()=" + getClass()
				+ ", hashCode()=" + hashCode() + ", toString()="
				+ super.toString() + "]";
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DataSaveLoad.load();
		mainFrame.login.addComponentListener(new LoginListen());
		mainPnl.setVisible(false);
		mainFrame.add(mainPnl);
		mainFrame.layout.putConstraint(SpringLayout.NORTH,
				mainPnl, 0, SpringLayout.NORTH, mainFrame);
		mainFrame.layout.putConstraint(SpringLayout.WEST, 
				mainPnl, 0, SpringLayout.WEST, mainFrame);
		mainFrame.layout.putConstraint(SpringLayout.EAST,
				mainPnl, -30, SpringLayout.EAST, mainFrame);
		mainFrame.layout.putConstraint(SpringLayout.SOUTH,
				mainPnl, -50, SpringLayout.SOUTH, mainFrame);
		
		mainPnl.addComponentListener(new LoginListen());
		// I don't know if you can implement this Zipp, but what if we were to put in the Main panel that will hold all the other panels a Back Button 
		// works similar to the following idea:
		//		JPanel prevPanel= Last Panel we showed.
		// When we change panels we just set prevPanel to this last panel each time then set a Back button to just display prevPanel.
		// Do you see a problem with this?
		// - Dolan
		
		//Theres no real way to put it in the Main Frame. The better way is to put a back button in every toolbar panel. So 
		//NursePanel, SystemAdminPanel, and PatientPanel, etc will have a back button. Thatll work better.
		//-Zipp
		mainFrame.validate();
		mainFrame.repaint();
		mainFrame.pack();
		mainFrame.setVisible(true);
		
	}
}
